var express = require("express");
var router = express.Router();
var router = express.Router();
var query = require('./mysql')


// 首页请求
// 轮播图的导航
router.post("/swipernav", async (req, res) => {
  let swiperlist = await query("select * from swipernav ");
  let json = {}
  if (swiperlist.length != 0) {
    json = {
      code: 200,
      result: swiperlist
    };
  } else {
    json = {
      code: 100
    }
  };
  res.send(json)
});

// 商品的分类

router.post("/content", async (req, res) => {
  const content = {
    one: [],
    two: [],
    thress: [],
    frou: []
  }
  let swiperlist = await query("select c.* from indexcontent as c, indextheam as t where c.theam= t.theam   ");
  let navlist = await query("select * from  indextheam");
  for (var i = 0; i < swiperlist.length; i++) {
    switch (swiperlist[i].theam) {
      case "礼拜五":
        content.one.push(swiperlist[i])
        break;
      case "新鲜水果":
        content.two.push(swiperlist[i])
        break;
      case "水产海鲜":
        content.thress.push(swiperlist[i])
        break;
      case "肉类蛋禽":
        content.frou.push(swiperlist[i])
        break;
    }
  }
  let json = {}
  if (swiperlist.length != 0) {
    json = {
      code: 200,
      result: content,
      theam: navlist
    };
  } else {
    json = {
      code: 100
    }
  };
  res.send(json)
});


// 同城请求

router.post("/same", async (req, res) => {
  let data = req.body.city;
  console.log(data);
  let rows = await query(`select * from samecity where address like '%${data}%'`);
  console.log(rows);
  let json = {}
  if (rows.length != 0) {
    json = {
      code: 200,
      result: rows,

    };
  } else {
    json = {
      code: 100
    }
  };
  res.send(json)
});

// 添加购物车请求


router.post("/addcar", async (req, res) => {
  let data = req.body.data;
  console.log(data);
  console.log(data.shopname, data.content, data.image, data.newprice, data.guige)
  let rows = await query(`insert into addcar(shopname,name,image,newprice,num,guige) values('${data.shopname}','${data.content}','${data.image}',${data.newprice},1,'${data.guige}')`);
  console.log(rows);
  let json = {}

  if (rows.length != 0) {
    json = {
      code: 200,
      result: rows,

    };
  } else {
    json = {
      code: 100
    }
  };
  res.send(json)
});













module.exports = router;